package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.entity.Customer;
import xyz.zhouzhaodong.crm.entity.CustomerTypeTime;
import xyz.zhouzhaodong.crm.mapper.CustomerMapper;
import xyz.zhouzhaodong.crm.mapper.CustomerTypeTimeMapper;
import xyz.zhouzhaodong.crm.service.ICustomerTypeTimeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;

/**
 * <p>
 * 客户状态操作时间 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-28
 */
@Service
public class CustomerTypeTimeServiceImpl extends ServiceImpl<CustomerTypeTimeMapper, CustomerTypeTime> implements ICustomerTypeTimeService {

    @Resource
    CustomerTypeTimeMapper customerTypeTimeMapper;

    @Resource
    CustomerMapper customerMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage delete(CustomerTypeTime customerTypeTime) {
        try{
            QueryWrapper<CustomerTypeTime> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("customer_id", customerTypeTime.getCustomerId());
            queryWrapper.eq("customer_type", "成交");
            customerTypeTimeMapper.delete(queryWrapper);
            // 将客户状态改为普通客户
            QueryWrapper<Customer> q = new QueryWrapper<>();
            q.eq("id", customerTypeTime.getCustomerId());
            Customer customer = new Customer();
            customer.setConnectType("普通客户");
            customerMapper.update(customer, q);
            return RestMessage.success();
        } catch (Exception e){
            return RestMessage.failed(e.getMessage());
        }
    }

}
